package com.cheng.io.writer;

import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;

/**
 * 文件字符输出流
 */
public class FileWriterTest01 {
    public static void main(String[] args) {
        try (
                // 构造器：FileWriter(String fileName) 每次创建流都会先清空文件数据 | FileWriter(String fileName, boolean append) append值为true每次创建流不会清楚数据
                Writer writer = new FileWriter("D:\\code\\java-study\\java-study\\io\\resources\\test03.txt")
        ) {
            // void write(char cbuf[])
            char[] chars = new char[]{'中','国'};
            writer.write(chars);
            // void write(char cbuf[], int off, int len) 从char数组中第off之后的元素开始写入len个数据
            writer.write("中国你好".toCharArray(),2,2);
            // void write(String str)
            writer.write("!");
            // void write(String str, int off, int len)
            writer.write("!!!\n123",3,4);

            //支持链式编程
            writer.append("\n").append("a").append("b").append("c");

            // flush stream
            writer.flush();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
